package com.toman.Adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.toman.MyApplication;
import com.toman.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/4/3.
 */

public class GridAdpter extends BaseAdapter {
    MyApplication myApplication;
    Context context;
    ArrayList<String> arrayList;
    LayoutInflater inflater;
    int month;

    public GridAdpter(Context context, ArrayList<String> arrayList, int month, MyApplication myApplication
    ) {
        this.context = context;
        this.arrayList = arrayList;
        this.month = month;
        inflater = LayoutInflater.from(context);
        this.myApplication = myApplication;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;
        if (null == convertView) {
            addPackage = new GridAdpter.AddPackage();
            convertView = inflater.inflate(R.layout.month_item, null, false);
            addPackage.title = (TextView) convertView.findViewById(R.id.mTitleMonth);

            convertView.setTag(addPackage);
        } else {
            addPackage = (AddPackage) convertView.getTag();
        }

        addPackage.title.setText(arrayList.get(position));

        if (myApplication.getSp().getString("token", "").equals("toman")) {
            if (position == month) {
                addPackage.title.setBackgroundResource(R.color.management);
            } else {
                addPackage.title.setBackgroundResource(R.color.transparent);
            }
        } else if (myApplication.getSp().getString("token", "").equals("operater")) {
            if (position == month) {
                addPackage.title.setBackgroundResource(R.color.m2);
            } else {
                addPackage.title.setBackgroundResource(R.color.transparent);
            }
        } else if (myApplication.getSp().getString("token", "").equals("company")) {
            if (position == month) {
                addPackage.title.setBackgroundResource(R.color.m1);
            } else {
                addPackage.title.setBackgroundResource(R.color.transparent);
            }
        }

        return convertView;
    }

    public class AddPackage {
        TextView title;
    }
}